using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;

namespace GetTogether.Utility
{
    public class Currency
    {
        public static decimal GetExchangeRate(string apiUrl, string apiKey, string fromCurrency, string toCurrency)
        {
            if (string.IsNullOrEmpty(fromCurrency) || string.IsNullOrEmpty(toCurrency)) return -1;
            if (fromCurrency == toCurrency) return 1;
            string url = string.Format("{0}{1}/{2}?k={3}", apiUrl, fromCurrency, toCurrency, apiKey);
            WebRequest wr = WebRequest.Create(url);
            WebResponse resp = wr.GetResponse();
            string s = "";
            using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
            {
                s = sr.ReadToEnd();
            }
            decimal ret = -1;
            if (decimal.TryParse(s, out ret)) return ret;
            return -1;
        }
    }
}
